<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>数字图书管理系统</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <style>
        nav {
            font-size: 1.5rem;
            background-color: #21D4FD;
            background-image: -webkit-linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
            background-image: -moz-linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
            background-image: -o-linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
            background-image: linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
        }
        h1 {
            font-size: 2rem;
            color: white;
        }
        li {
            margin: 10px 20px;
        }
        th {
            width: 100px;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-primary navbar-dark">
    <ul class="navbar-nav">
        <li>
            <h1><a th:href="@{/}" style="color: white; text-decoration: none">数字图书管理系统</a></h1>
        </li>
        <li class="nav-item">
            <a class="nav-link active" th:href="@{/user/book/getAll}">图书信息</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" th:href="@{/user/record/getOwnRecord}">借阅记录</a>
        </li>
    </ul>
    <div class="text-white" style="font-size: medium; position: absolute; bottom: 30px; right: 40px">
        <div class="dropdown">
            <a class="dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
               th:text="${session.name}+', 您好'">
            </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a class="dropdown-item" th:href="@{/user/toInfoPage}" sec:authorize="hasRole('USER')">个人信息</a>
                <a class="dropdown-item" th:href="@{/toChPwdPage}">修改密码</a>
                <a class="dropdown-item" th:href="@{/logout}">注销</a>
            </div>
        </div>
    </div>
</nav>

<div class="container">
    <table class="table">
        <tr>
            <th>书名</th>
            <td th:text="${book.getName()}"></td>
        </tr>
        <tr>
            <th>作者</th>
            <td th:text="${book.getAuthor()}"></td>
        </tr>
        <tr>
            <th>出版社</th>
            <td th:text="${book.getPublish()}"></td>
        </tr>
        <tr>
            <th>ISBN</th>
            <td th:text="${book.getIsbn()}"></td>
        </tr>
        <tr>
            <th>简介</th>
            <td th:text="${book.getIntroduction()}"></td>
        </tr>
        <tr>
            <th>语言</th>
            <td th:text="${book.getLanguage()}"></td>
        </tr>
        <tr>
            <th>价格</th>
            <td th:text="${book.getPrice()}"></td>
        </tr>
        <tr>
            <th>出版日期</th>
            <td th:text="${#dates.format(book.getPubdate(),'yyyy-MM-dd')}"></td>
        </tr>
        <tr>
            <th>分类</th>
            <td th:text="${book.getClassName()}"></td>
        </tr>
        <tr>
            <th>书架号</th>
            <td th:text="${book.getPressmark()}"></td>
        </tr>
        <tr>
            <th>文件</th>
            <td>
                <a th:if="${book.getIsBuy() == 1}" th:href="${book.getFilepath()}"><span  style="color: red">已购买，文档可预览下载</span></a>
                <a th:if="${(book.getIsBuy() == 0) && (book.getPrice() != 0)}" th:href="@{/pay/alipay(dona_id=1,dona_money=${book.getPrice()},bookid=${book.getBookId()},
                bookname=${book.getName()},username=${session.username})}"><span style="color: deepskyblue">暂无权限下载，要获取该文档请先购买</span></a>
                <a th:if="${(book.getPrice() == 0) && (book.isLend != 1)}" th:href="@{/user/record/addOwnRecord(bookid=${book.getBookId()})}"><span  style="color: red">该书免费，可直接借阅</span></a>
                <a th:if="${(book.getPrice() == 0) && (book.isLend == 1)}" th:href="${book.getFilepath()}"><span  style="color: red">该书已被借阅，点击可查看该书</span></a>
                <br/>
                <a th:if="${(book.getPrice() == 0) && (book.isLend == 1)}" th:href="@{/user/record/returnBook(bookid=${book.getBookId()})}"><span  style="color: forestgreen">点此退还</span></a>
            </td>
        </tr>
        <tr>
        <td>
            <button id="btnLike" type="button" class="btn btn-danger btn-xs">点赞
                <span th:text="${book.likeCount}"></span>
            </button>
        </td>
        <td>
            <button id="btnCollect" type="button" class="btn btn-warning btn-xs">收藏</button>
        </td>
        </tr>
        <div>
            <form>
                <tr th:each="info:${book.getRemarkList()}">
                    <td>
                        <span th:text="${info.getName()}+':'" style="color:blueviolet;"></span>
                    </td>
                    <td>
                        <p th:text="${info.getRemark()}" style="color: #1e7e34"></p>
                    </td>
                </tr>
            </form>
        </div>
        <tr>
            <th>留言:</th>
            <td>
            <form th:action="@{/user/book/remark}">
                <input type="text" name="remark" style="width: 500px">
                <input type="text"  name="username" style="display:none" th:value="${session.username}">
                <input type="text"  name="bookId" style="display:none" th:value="${book.bookId}">
                <input type="submit" value="提交">
            </form>
            </td>
        </tr>
    </table>
    <a th:href="@{/user/book/getAll}" class="btn btn-outline-primary btn-lg">返&nbsp;&nbsp;&nbsp;&nbsp;回</a>
</div>

<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript">
    console.log("123");
    $("#btnLike").click(function () {
        console.log("btnLike");
        if ($.cookie("likeId") != [[${book.bookId}]]) {
            $.ajax({
                async: false,
                type: "POST",
                url: "/user/book/like",
                data: {bookId: [[${book.bookId}]]},
                // dataType: "text",
                success: function (data) {
                    $("#btnLike").html(data);
                    $("#btnLike").attr("disabled","disabled");
                    $.cookie(
                        "likeId",
                        [[${book.bookId}]],//需要cookie写入的业务
                        {
                            "path": "/", //cookie的默认属性
                        }
                    );
                },
                error: function () {
                    alert("获取数据出错!");
                },
            });
        }
    });


    $("#btnCollect").click(function () {
        console.log("btnCollect");
            $.ajax({
                async: false,
                type: "POST",
                url: "/user/book/collect",
                data: {
                    bookId: [[${book.bookId}]],
                    username: [[${session.username}]]
                },
                // dataType: "text",
                success: function (data) {
                    $("#btnCollect").html(data);
                    $("#btnCollect").attr("disabled","disabled");
                },
                error: function () {
                    alert("获取数据出错!");
                },
            });
    });

    // $("#btnRemark").click(function () {
    //     console.log("btnRemark");
    //     $.ajax({
    //         async: false,
    //         type: "POST",
    //         url: "/user/book/remark",
    //         data: {
    //             bookId: [[${book.bookId}]],
    //             username: [[${session.username}]]
    //         },
    //         // dataType: "text",
    //         success: function (data) {
    //             $("#btnCollect").html(data);
    //             $("#btnCollect").attr("disabled","disabled");
    //         },
    //         error: function () {
    //             alert("获取数据出错!");
    //         },
    //     });
    // });
</script>
</body>
</html>